package com.ymatou.iapi.sellerproduct.testcase;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.sellerproduct.service.BatchSetProductOffSaleCall;
import com.ymatou.iapi.sellerproduct.service.BatchSetProductOnSaleCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.NewJavaMongoCheckService;
import com.ymttest.business.service.SellerProductCallService;
import com.ymttest.database.mongo.YmtMongoDriverFactroy;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.MapUtil;
import com.ymttest.utils.YMTDateUtil;

@TestSuite
public class Ts_BatchSetProductOnSale{
	private static BatchSetProductOnSaleCall batchSetProductOnSaleCall;
	private static BatchSetProductOffSaleCall batchSetProductOffSaleCall;
	private static Integer sellerId = Integer.valueOf(EnvSetup.getData("productsellerid"));
	private static String sellerName = EnvSetup.getData("productsellername");	
	private static Integer sellerId1 = Integer.valueOf(EnvSetup.getData("productsellerid4"));
	private static String sellerName1 = EnvSetup.getData("productsellername4");	
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("批量上架商品");
	}

	@Before
	public void caseUp() {
		batchSetProductOnSaleCall=new BatchSetProductOnSaleCall();
		batchSetProductOffSaleCall=new BatchSetProductOffSaleCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(batchSetProductOnSaleCall.getOpurl());
	}
	
	

	
	
	@After
	public void caseDown() {
		Logger.end();
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_001() {
		Logger.start(true,"上架多个商品");
		try {
			//TODO 补全case代码
	
			//获取当前时间&结束时间
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
			String endTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(7), 0);
			
			//发布一个立即上架商品
			String productID1 =  SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");
			String productID2 =  SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");			
			
			List<String> productidList=new ArrayList<>();
			productidList.add(productID1);
			productidList.add(productID2);
			
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("ProductIds", new JSONArray(productidList));	
			maplist.put("UserId",sellerId);			
			
			//先下架商品
			batchSetProductOffSaleCall.callAndGetReturnData(maplist);
			
			//再Call 批量上架接口
			JSONObject result = batchSetProductOnSaleCall.callAndGetReturnData(maplist);
			
			//断言返回报文
			batchSetProductOnSaleCall.getreturncodecheck(result, productID1);
			
			//验证重新商家后，销售开始时间为当前时间，结束时间为7天后
			batchSetProductOnSaleCall.setOnSalecheck(startTime, endTime, productidList);						
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID1);
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID2);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_002() {
		Logger.start(true,"上架一个发布但未从未上架商品");
		try {
			//TODO 补全case代码
	
			//获取当前时间&结束时间
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
			String endTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(7), 0);
			
			//发布一个立即上架商品
			String productID1 =  SellerProductCallService.addNewProductNotOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");		
			
			List<String> productidList=new ArrayList<>();
			productidList.add(productID1);
			
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("ProductIds", new JSONArray(productidList));	
			maplist.put("UserId",sellerId);			
			
			//先下架商品
			batchSetProductOffSaleCall.callAndGetReturnData(maplist);
			
			//再Call 批量上架接口
			JSONObject result = batchSetProductOnSaleCall.callAndGetReturnData(maplist);
			
			//断言返回报文
			batchSetProductOnSaleCall.getreturncodecheck(result, productID1);
			
			//验证重新商家后，销售开始时间为当前时间，结束时间为7天后
			batchSetProductOnSaleCall.setOnSalecheck(startTime, endTime, productidList);						
			
			//首次露出时间、结束时间断言
			SellerProductCallService.checkListingTime(productID1, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID1);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_003() {
		Logger.start(true,"上架一个定时上架商品");
		try {
			//TODO 补全case代码
	
			//获取当前时间&结束时间
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
			String endTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(7), 0);
			
			//发布一个立即上架商品
			String productID1 =  SellerProductCallService.addNewProductOnTime(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");		
			
			List<String> productidList=new ArrayList<>();
			productidList.add(productID1);
			
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("ProductIds", new JSONArray(productidList));	
			maplist.put("UserId",sellerId);			
			
			//先下架商品
			batchSetProductOffSaleCall.callAndGetReturnData(maplist);
			
			//再Call 批量上架接口
			JSONObject result = batchSetProductOnSaleCall.callAndGetReturnData(maplist);
			
			//断言返回报文
			batchSetProductOnSaleCall.getreturncodecheck(result, productID1);
			
			//验证重新商家后，销售开始时间为当前时间，结束时间为7天后
			batchSetProductOnSaleCall.setOnSalecheck(startTime, endTime, productidList);						
			
			//首次露出时间、结束时间断言
			SellerProductCallService.checkListingTime(productID1, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID1);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}		
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_004() {
		Logger.start(true,"上架一个FBX商品");
		try {
			//TODO 补全case代码
	
			//获取当前时间&结束时间
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
			String endTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(7), 0);
			
			//发布一个立即上架FBX商品
			String productID1 =  SellerProductCallService.addFBXProduct(MapUtil.hashMap("UserID", sellerId , "LoginID", sellerName));		
			
			List<String> productidList=new ArrayList<>();
			productidList.add(productID1);
			
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("ProductIds", new JSONArray(productidList));	
			maplist.put("UserId",sellerId);			
			
			//先下架商品
			batchSetProductOffSaleCall.callAndGetReturnData(maplist);
			
			//再Call 批量上架接口
			JSONObject result = batchSetProductOnSaleCall.callAndGetReturnData(maplist);
			
			//断言返回报文
			batchSetProductOnSaleCall.getreturncodecheck(result, productID1);
			
			//验证重新商家后，销售开始时间为当前时间，结束时间为7天后
			batchSetProductOnSaleCall.setOnSalecheck(startTime, endTime, productidList);						
			
			//首次露出时间、结束时间断言
			SellerProductCallService.checkListingTime(productID1, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID1);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}		
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_005() {
		Logger.start(true,"已上架商品再次上架");
		try {
			//TODO 补全case代码
	
			//获取当前时间&结束时间
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
			String endTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(7), 0);
			
			//发布一个立即上架商品
			String productID1 =  SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null).getJSONObject("Data").getString("ProductID");	
			
			List<String> productidList=new ArrayList<>();
			productidList.add(productID1);
			
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("ProductIds", new JSONArray(productidList));	
			maplist.put("UserId",sellerId);			
			
			//先下架商品
			batchSetProductOffSaleCall.callAndGetReturnData(maplist);
			
			//再Call 批量上架接口
			batchSetProductOnSaleCall.callAndGetReturnData(maplist);
			//call第2次
			JSONObject result = batchSetProductOnSaleCall.callAndGetReturnData(maplist);
			
			//断言返回报文
			batchSetProductOnSaleCall.getreturncodecheck(result, productID1);
			
			//验证重新商家后，销售开始时间为当前时间，结束时间为7天后
			batchSetProductOnSaleCall.setOnSalecheck(startTime, endTime, productidList);						
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID1);
			}
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}	
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_006() {
		Logger.start(true,"上架售罄商品");
		try {
			//TODO 补全case代码
	
			//获取当前时间&结束时间
			String startTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(0), 0);
			String endTime = YMTDateUtil.getBeforeOrNextDayStr(YMTDateUtil.getBeforeOrNextDay(7), 0);
			
			//发布一个立即上架商品
			JSONObject results =  SellerProductCallService.addNewProductOnSale(sellerId, sellerName, null);
			String productID1 = results.getJSONObject("Data").getString("ProductID");	
			String productnameforrequest = results.getJSONObject("JsonBeanRequest").getJSONObject("Product").getString("ProductName");
					
			//组装上架商品请求报文
			List<String> productidList=new ArrayList<>();
			productidList.add(productID1);
			
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("ProductIds", new JSONArray(productidList));	
			maplist.put("UserId",sellerId);			
			
			//先下架商品
			batchSetProductOffSaleCall.callAndGetReturnData(maplist);
			
			//更新库存
			SellerProductCallService.updateAllStockNumAs0(sellerId, productID1);
			
			//再Call 批量上架接口
			JSONObject dataStr = batchSetProductOnSaleCall.callAndGetReturnData(maplist);
			
			//断言返回报文
			String code =  dataStr.getString("Code");
			Logger.verifyIsContains(200, code, "\"Code\":200");

			String msg =  dataStr.getJSONObject("Data").getJSONArray("RspMsg").getJSONObject(0).getString("Msg");
			Logger.verifyIsContains("商品库存为0，不能上架", msg, "商品库存为0，不能上架");

			String productnameforresponse =  dataStr.getJSONObject("Data").getJSONArray("RspMsg").getJSONObject(0).getString("ProductName");
			Logger.verifyEquals(productnameforrequest, productnameforresponse, "无法上架商品的商品名称");
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productID1);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Ignore
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_007() {
		Logger.start(true,"商品id为空");
		try {
			//TODO 补全case代码
			//组装下架请求报文
			List<String> productidList=new ArrayList<>();
			productidList.add(null);
			
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("ProductIds", new JSONArray(productidList));	
			maplist.put("UserId",sellerId);			
						
			//再Call 批量上架接口
			JSONObject dataStr = batchSetProductOnSaleCall.callAndGetReturnData(maplist);
						
			//错误断言
			String code =  dataStr.getString("Code");
			Logger.verifyIsContains(201, code, "\"Code\":201");
			String bCode =  dataStr.getString("BCode");
			Logger.verifyIsContains(201, bCode, "\"BCode\":201");
			String msg =  dataStr.getString("Msg");
			Logger.verifyIsContains("上架商品列表中有不属于当前用户的商品", msg, "上架商品列表中有不属于当前用户的商品");
			String trace =  dataStr.getString("Trace");
			Logger.verifyIsNull(trace, "Trace等于空");	
			String data =  dataStr.getString("Data");
			Logger.verifyIsNull(data, "Data等于空");	
			

		} catch (Exception e) {
			Logger.fail(e);
		}
	}		
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_008() {
		Logger.start(true,"listingtime时间已过，立即上架不改变listingtime");
		try {

			//发布一个立即上架商品
			String productid = SellerProductCallService.addNewProductOnSale(sellerId1, sellerName1, null).getJSONObject("Data").getString("ProductID");
			
			//上架
			List<String> productidList=new ArrayList<>();
			productidList.add(productid);
			
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("ProductIds", new JSONArray(productidList));	
			maplist.put("UserId",sellerId1);		
			JSONObject response = batchSetProductOnSaleCall.callAndGetReturnData(maplist);
			
			//验证listingtime
			SellerProductCallService.checkSqlListingTime(productid, 1);
			SellerProductCallService.checkListingTime(productid, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}		

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_BatchSetProductOnSale_009() {
		Logger.start(true,"listingtime时间未过，立即上架listingtime为vaildstart");
		try {

			//发布一个不上架商品
			String productid = SellerProductCallService.addNewProductNotOnSale(sellerId1, sellerName1, null).getJSONObject("Data").getString("ProductID");
			
			//上架
			List<String> productidList=new ArrayList<>();
			productidList.add(productid);
			
			Map<String,Object> maplist = new HashMap<String, Object>();		    
			maplist.put("ProductIds", new JSONArray(productidList));	
			maplist.put("UserId",sellerId1);		
			JSONObject response = batchSetProductOnSaleCall.callAndGetReturnData(maplist);
			
			//验证listingtime
			SellerProductCallService.checkSqlListingTime(productid, 1);
			SellerProductCallService.checkListingTime(productid, null);
			
			if (YmtMongoDriverFactroy.isNewJavaMgoCheck()){
				NewJavaMongoCheckService.checkProductMgoSyncBySQL_NewJavaMgo(productid);
			}
		} catch (Exception e) {
			Logger.fail(e);
		}
	}		
	
}